package com.wenxixuanwu.fruitstore.controller;

import com.wenxixuanwu.fruitstore.mapper.BackBannerMapper;
import com.wenxixuanwu.fruitstore.service.exception.FileEmptyException;
import com.wenxixuanwu.fruitstore.service.exception.FileSizeException;
import com.wenxixuanwu.fruitstore.service.exception.FileTypeException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author :小凡凡
 * @version :1.0
 * @Project :fruitstore
 * @Date :2022/4/24
 */
@RestController
public class UploadController extends BaseController{
    /** 头像文件上传的大小(10MB) */
    public static final int AVATAR_MAX_SIZE = 10 * 1024 * 1024;
    /** 允许上传的头像的文件类型 */
    public static final List<String> AVATAR_TYPES = new ArrayList<String>();
    /** 初始化允许上传的头像的文件类型 */
    static{
        AVATAR_TYPES.add("image/jpeg");
        AVATAR_TYPES.add("image/png");
        AVATAR_TYPES.add("image/jpg");
        AVATAR_TYPES.add("image/bmp");
        AVATAR_TYPES.add("image/gif");
    }
    @Value("${BannerSavePath.BannerfileUrl}")
    private String BannerFileUrl; //图标的物理存储路径

    @Value("${BannerSavePath.BannerfileUrlMapper}")
    private String BannerFileUrlMapper; // 图标映射文件

    @Value("${BannerSavePath.ProductfileUrl}")
    private String ProductFileUrl; //图标的物理存储路径

    @Value("${BannerSavePath.ProductfileUrlMapper}")
    private String ProductFileUrlMapper; // 图标映射文件

    @PostMapping("/uploadProduct")
    public String profilePhotoUploadProduct(@RequestParam("file") MultipartFile fileUpload){
        String fileName = checkPhotoUpload(fileUpload);
        try {
            fileUpload.transferTo(new File(ProductFileUrl+fileName));
            return ProductFileUrlMapper + fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @PostMapping("/uploadBanner")
    public String profilePhotoUpload(@RequestParam("file") MultipartFile fileUpload){
        String fileName = checkPhotoUpload(fileUpload);
        try {
            fileUpload.transferTo(new File(BannerFileUrl+fileName));
            return BannerFileUrlMapper + fileName;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @GetMapping("/remove")
    public void remove(String filename){
        String filePath = BannerFileUrl + filename;
        new File(filePath).delete();
    }

    private String checkPhotoUpload(MultipartFile fileUpload){
        if (fileUpload.getSize() > AVATAR_MAX_SIZE){
            throw new FileSizeException("不允许上传" + (AVATAR_MAX_SIZE)/1024 + "KB的头像文件");
        }
        String contentType = fileUpload.getContentType();//获取文件上传的文件类型
        if (!AVATAR_TYPES.contains(contentType)){
            throw new FileTypeException("不支持使用该类型的文件做为头像");
        }
        //获取文件名
        String fileName = fileUpload.getOriginalFilename();
        //获取文件后缀名，规定特定的图片才能上传，否则拒绝保存
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //为了避免发生图片替换，这里使用了文件名重新生成
        fileName = UUID.randomUUID().toString().replaceAll("-","")+ suffixName;

        return fileName;
    }
}
